<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/14
 * Time: 9:11
 */

namespace app\admin\controller;


use app\reuse\controller\ResponseJson;
use Redis\Redis;
use think\Db;

class Imdata extends Common
{
    use ResponseJson;

    /**
     * @return mixed|string
     * @throws \think\exception\DbException
     * @context 敏感词汇列表
     */
    function sensitive_word_index()
    {
        if(input("get.action")!='ajax'){
            return $this->fetch();
        }else{
            $where=" id>0 ";
            $searchParams=input("post.searchParams");
            if(!empty($searchParams['search'])){
                $where.=" and words like '%".$searchParams['search']."%'";
            }
            $listRows=empty(input("post.limit"))?10:input("post.limit");
            $list=Db::table("sensitive_word")
                ->where($where)
                ->order("id desc")
                ->paginate($listRows,false,['query'=>request()->param()]);
            return $this->jsonSuccessData($list);
        }
    }

    /**
     * @return array
     * @context 敏感词汇添加
     */
    function sensitive_word_add()
    {
        $words=input("post.words");
       $res=Db::table("sensitive_word")->insert(array("words"=>$words));
       if($res){
           $redis = new Redis();
           $info = Db::table('sensitive_word')->field('words')->select();
           $redis->hSetJson('sensitive', 'word',$info);
           return array("status"=>true);
       }else{
           return array("status"=>false);
       }
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @context 敏感词汇修改
     */
    function sensitive_word_write()
    {
        $param=input("post.");
        Db::table("sensitive_word")->where(array("id"=>$param['id']))->update(array($param['field']=>$param['value']));
        $redis = new Redis();
        $info = Db::table('sensitive_word')->field('words')->select();
        $redis->hSetJson('sensitive', 'word',$info);
        return array("status"=>true);
    }

    /**
     * @return mixed|string
     * @throws \think\exception\DbException
     * @context 敏感词使用人
     */
    function sensitive_use_index()
    {
        if(input("get.action")!='ajax'){
            $sensitive_id=input("get.id");
            $this->assign("id",$sensitive_id);
            return $this->fetch();
        }else{
            $listRows=empty(input("post.limit"))?10:input("post.limit");
            $sensitive_id=input("get.id");
            $searchParams=input("post.searchParams");
            $where=null;
            if(!empty($searchParams['search'])){
                $where.=" and (m.code like '%".$searchParams['search']."%' 
                or m.name like '%".$searchParams['search']."%' 
                or m.phone like '%".$searchParams['search']."%')";
            }
            $list=Db::table("log_sensitive_use lsu")
                ->field("m.code,m.name,m.phone,if(m.sex=1,'男','女') sex,m.age")
                ->join("member m","m.code=lsu.member_code {$where}")
                ->where(array("sensitive_id"=>$sensitive_id))
                ->paginate($listRows,false,['query'=>request()->param()]);
            return $this->jsonSuccessData($list);
        }
    }

    function chat_activity_static()
    {
        if(input("get.action")!='ajax'){
            return $this->fetch();
        }else{
            $date=date("Y-m-d");
            $_show_date=$date;
            $_search_date=input("post.search_date");
            if(!empty($_search_date)){
                $_show_date=$_search_date;
            }
            $list=Db::table("chat_activity")->where(array("chat_date"=>$_show_date))->order("chat_start")->select();
            if($_show_date==$date){
                $_redis=new Redis();
                $_hour=date("H");
                if($_hour%2==1){
                    $start=$_hour-1;
                    $end=$_hour+1;
                }else{
                    $start=$_hour;
                    $end=$_hour+2;
                }
                $_now_num=$_redis->hGet($_show_date,$start.'to'.$end);
                if(!empty($_now_num)){
                    $_now_list=array("chat_date"=>$_show_date,"chat_start"=>$start,"chat_end"=>$end,"activity_num"=>$_now_num);
                    array_push($list,$_now_list);
                }
            }
            return array("status"=>true,"date_array"=>array_column($list,"chat_start"),
                "data_array"=>array("name"=>"参与人数","type"=>'line',"data"=>array_column($list,'activity_num')));
        }
    }

    function chat_records()
    {
//                Db::connect("db_config2")->table("chat")
//            ->where(array("main"=>"1"))
//            ->delete();
//        Db::connect("db_config2")->table("chat")
//            ->where(array("main"=>'2'))
//            ->delete();
//        $list=Db::connect("db_config2")->table("chat")->select();
//        dump($list);die;
        if(input("get.action")!='ajax'){
            return $this->fetch();
        }else{
            $member_id=input("get.member_id");
            if(!empty($member_id)){
                $list=Db::connect("db_config2")->table("chat")->where(array("main"=>$member_id))
                    ->paginate(10,false,['query'=>request()->param()]);
                $_list=$list->toArray();
//                dump($_list);
                foreach($_list['data'] as $k=>$v){
                    $content = json_decode($v['content'],true);
//                    dump($content);
                    $_end=end($content['msgData']);
                    $_list['data'][$k]['end_time']=$_end['sendTime'];
                }
                return $this->jsonSuccessData($_list);
            }else{
                return $this->jsonData("00000");
            }
        }
    }
    /**
     *时间戳 转   日期格式 ： 精确到毫秒，x代表毫秒
     */
    function get_microtime_format($time)
    {
        if(strstr($time,'.')){
            sprintf("%01.3f",$time); //小数点。不足三位补0
            list($usec, $sec) = explode(".",$time);
            $sec = str_pad($sec,3,"0",STR_PAD_RIGHT); //不足3位。右边补0
        }else{
            $usec = $time;
            $sec = "000";
        }
        $date = date("Y-m-d H:i:s",$usec);
        return str_replace("x", $sec, $date);
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @context 搜索用户
     */
    function searchMember()
    {
        $search=input("post.search");
        $list=Db::table("member")->field("id,name,phone,code")
            ->where("name like '%".$search."%' or phone like '%".$search."%' or code like '%".$search."%'")
            ->select();
        if(!empty($list)){
            $str="
            <table class=\"layui-table layui-form\">
                <thead>
                    <th>姓名</th>
                    <th>手机号</th>
                    <th>ID</th>
                </thead>
                <tbody>";
            foreach($list as $k=>$v){
                $str.="<tr onclick='confirmMember(\"{$v['code']}\")'>
                        <td>{$v['name']}</td>
                        <td>{$v['phone']}</td>
                        <td>{$v['code']}</td>
                    </tr>";
            }
            $str.="</tbody>
            </table>
        ";
            return array("status"=>true,"data"=>$str);
        }else{
            return array("status"=>false);
        }
    }

    /**
     * @return mixed
     * @throws \think\Exception
     * @context 聊天记录列表
     */
    function chat_records_list()
    {
        $id=input("get.id");
        $list=Db::connect("db_config2")->table("chat")->where(array("id"=>$id))->find();
        $list['content'] = json_decode($list['content'],true);
//        dump($list);
        $this->assign("list",$list);
        return $this->fetch();
    }

    /**
     * @return array
     * @throws \think\Exception
     * @context 聊天记录删除
     */
    function chat_records_delete()
    {
        $id=input("post.id");

        Db::connect("db_config2")->table("chat")->where(array("id"=>$id))->delete();
        return array("status"=>true);
    }
}